#include "crecipefilter.h"

using namespace cooking;

CNutritionFilter::ERecipeFilterType CNutritionFilter::s_recipe_filter_type = CNutritionFilter::EPerServe;

CRecipeFilter::CRecipeFilter(QObject *parent) :
    QObject(parent)
{
}

CRecipeFilter::CRecipeFilter(const CRecipeFilter& other):QObject(NULL) {
    copy(other);
}

CRecipeFilter& CRecipeFilter::operator =(const CRecipeFilter& other) {
    copy(other);
    return *this;
}

bool CRecipeFilter::operator ==(const CRecipeFilter& other) {
    if (m_ingredient != other.m_ingredient) {
        return false;
    }
    if (m_cuisine != other.m_cuisine) {
        return false;
    }

    if (m_technique != other.m_technique) {
        return false;
    }

    if (m_flavour != other.m_flavour) {
        return false;
    }

    if (m_dish_type != other.m_dish_type) {
        return false;
    }

    if (m_food_type != other.m_food_type) {
        return false;
    }

    if (m_ingredient_category != other.m_ingredient_category) {
        return false;
    }

    if (m_ready_time != other.m_ready_time) {
        return false;
    }

    if (m_nutrition != other.m_nutrition) {
        return false;
    }
    return true;
}

bool CRecipeFilter::operator !=(const CRecipeFilter& other) {
    return !(*this == other);
}

int CRecipeFilter::addIngredient(int ingredientId, EFilterType filterType) {
    addFilter(m_ingredient, ingredientId, filterType);
    return KOk;
}

void CRecipeFilter::deleteIngredient(int ingredientId) {
    deleteFilter(m_ingredient, ingredientId);
}

QList<int> CRecipeFilter::ingredients(EFilterType filterType) {
    return filters(m_ingredient, filterType);
}


int CRecipeFilter::addExistIngredient(int ingredientId) {
    if (!m_exist_ingredient.contains(ingredientId)) {
        m_exist_ingredient.append(ingredientId);
    }
    return KOk;
}

void CRecipeFilter::deleteExistIngredient(int ingredientId) {
    int index = m_exist_ingredient.indexOf(ingredientId);
    if (index != -1) {
        m_exist_ingredient.removeAt(index);
    }
}

QList<int> CRecipeFilter::existIngredients() {
    return m_exist_ingredient;
}

int CRecipeFilter::addCuisine(int cuisineId, EFilterType filterType) {
    addFilter(m_cuisine, cuisineId, filterType);
    return KOk;
}

void CRecipeFilter::deleteCuisine(int cuisineId) {
    deleteFilter(m_cuisine, cuisineId);
}

QList<int> CRecipeFilter::cuisines(EFilterType filterType) {
    return filters(m_cuisine, filterType);
}

int CRecipeFilter::addTechnique(int techniqueId, EFilterType filterType) {
    addFilter(m_technique, techniqueId, filterType);
    return KOk;
}

void CRecipeFilter::deleteTechnique(int techniqueId) {
    deleteFilter(m_technique, techniqueId);
}

QList<int> CRecipeFilter::techniques(EFilterType filterType) {
    return filters(m_technique, filterType);
}

int CRecipeFilter::addFlavour(int flavourId, EFilterType filterType) {
    addFilter(m_flavour, flavourId, filterType);
    return KOk;
}

void CRecipeFilter::deleteFlavour(int flavourId) {
    deleteFilter(m_flavour, flavourId);
}

QList<int> CRecipeFilter::flavours(EFilterType filterType) {
    return filters(m_flavour, filterType);
}

int CRecipeFilter::addDishType(int dishTypeId, EFilterType filterType) {
    addFilter(m_dish_type, dishTypeId, filterType);
    return KOk;
}

void CRecipeFilter::deleteDishType(int dishTypeId) {
    deleteFilter(m_dish_type, dishTypeId);
}

QList<int> CRecipeFilter::dishTypes(EFilterType filterType) {
    return filters(m_dish_type, filterType);
}

int CRecipeFilter::addFoodType(int foodTypeId, EFilterType filterType) {
    addFilter(m_food_type, foodTypeId, filterType);
    return KOk;
}

void CRecipeFilter::deleteFoodType(int foodTypeId) {
    deleteFilter(m_food_type, foodTypeId);
}

QList<int> CRecipeFilter::foodTypes(EFilterType filterType) {
    return filters(m_food_type, filterType);
}

int CRecipeFilter::addIngredientCategory(int categoryId, EFilterType filterType) {
    addFilter(m_ingredient_category, categoryId, filterType);
    return KOk;
}

void CRecipeFilter::deleteIngredientCategory(int categoryId) {
    deleteFilter(m_ingredient_category, categoryId);
}

QList<int> CRecipeFilter::ingredientCategory(EFilterType filterType) {
    return filters(m_ingredient_category, filterType);
}

int CRecipeFilter::addReadyTime(int time, EMoreLessType moreLessType) {
    deleteReadyTime(moreLessType);
    deleteReadyTime(EEqual);
    addFilter(m_ready_time, time, moreLessType);
    return KOk;
}

void CRecipeFilter::deleteReadyTime(EMoreLessType moreLessType) {
    deleteFilter(m_ready_time, moreLessType);
}

int CRecipeFilter::readyTimes(EMoreLessType moreLessType) {
    QList<int> list = filters(m_ready_time, moreLessType);
    if (list.count()) {
        // should have only one
        return list[0];
    }
    else {
        return KInvalidId;
    }
}

int CRecipeFilter::addNutrition(int nutritionId, EMoreLessType moreLessType,
                                double quantity) {
    CNutritionFilter filter;
    filter.setNutritionId(nutritionId);
    filter.setMoreLess(moreLessType);
    filter.setQuantity(quantity);

    addFilter(m_nutrition, nutritionId, filter);
    return KOk;
}

void CRecipeFilter::deleteNutrition(int nutritionId) {
    deleteFilter(m_nutrition, nutritionId);
}

QList<CNutritionFilter> CRecipeFilter::nutritions() {
    return filters(m_nutrition);
}

QMap<int, CRecipeFilter::EFilterType>::iterator CRecipeFilter::addFilter(QMap<int, EFilterType> & filterMap,
                                                                         int filterId, EFilterType& filterType) {

    return filterMap.insert(filterId, filterType);
}

QMap<int, CRecipeFilter::EMoreLessType>::iterator CRecipeFilter::addFilter(QMap<int, EMoreLessType> & filterMap,
                                                                           int filterId, EMoreLessType& moreLessType) {
    return filterMap.insert(filterId, moreLessType);
}

QMap<int, CNutritionFilter>::iterator
CRecipeFilter::addFilter(QMap<int, CNutritionFilter> & filterMap,
                         int filterId, CNutritionFilter& filter) {
    return filterMap.insert(filterId, filter);
}

int CRecipeFilter::deleteFilter(QMap<int, EFilterType> & filterMap,
                                int filterId) {
    return filterMap.remove(filterId);
}

int CRecipeFilter::deleteFilter(QMap<int, EMoreLessType> & filterMap,
                                int filterId) {
    return filterMap.remove(filterId);
}

int CRecipeFilter::deleteFilter(QMap<int, EMoreLessType> & filterMap,
                                EMoreLessType filterType) {
    QList<int> keys = filterMap.keys(filterType);
    for (int i = 0; i < keys.count(); i++) {
        filterMap.remove(keys[i]);
    }
    return KOk;
}

int CRecipeFilter::deleteFilter(QMap<int, CNutritionFilter> & filterMap,
                                int filterId) {
    return filterMap.remove(filterId);
}

QList<int> CRecipeFilter::filters(QMap<int, EFilterType> & filterMap,
                                  EFilterType filterType) {
    return filterMap.keys(filterType);
}

QList<int> CRecipeFilter::filters(QMap<int, EMoreLessType> & filterMap,
                                  EMoreLessType moreLessType) {
    return filterMap.keys(moreLessType);
}

QList<CNutritionFilter>
CRecipeFilter::filters(QMap<int, CNutritionFilter> & filterMap) {
    return filterMap.values();
}

void CRecipeFilter::copy(const CRecipeFilter& other) {
    m_ingredient = other.m_ingredient;
    m_cuisine = other.m_cuisine;
    m_technique = other.m_technique;
    m_flavour = other.m_flavour;
    m_dish_type = other.m_dish_type;
    m_food_type = other.m_food_type;
    m_ingredient_category = other.m_ingredient_category;
    m_ready_time = other.m_ready_time;
    m_nutrition = other.m_nutrition;
}


CNutritionFilter::CNutritionFilter() {

}

CNutritionFilter::CNutritionFilter(const CNutritionFilter& other):QObject(NULL) {
    *this = other;
}

CNutritionFilter& CNutritionFilter::operator=(const CNutritionFilter& other) {
    m_nutrition_id = other.m_nutrition_id;
    m_more_less = other.m_more_less;
    m_quantity = other.m_quantity;
//    m_unit_type_id = other.m_unit_type_id;
    return *this;
}

bool CNutritionFilter::operator==(const CNutritionFilter& other) const {
    if (m_nutrition_id != other.m_nutrition_id) {
        return false;
    }

    if (m_more_less != other.m_more_less) {
        return false;
    }

    if (m_quantity != other.m_quantity) {
        return false;
    }

//    if (m_unit_type_id != other.m_unit_type_id) {
//        return false;
//    }
    return true;
}

int CNutritionFilter::nutritionId() {
    return m_nutrition_id;
}

void CNutritionFilter::setNutritionId(int nutritionId){
    if (m_nutrition_id != nutritionId) {
        m_nutrition_id = nutritionId;
    }
}

CRecipeFilter::EMoreLessType CNutritionFilter::moreLess() {
    return m_more_less;
}

void CNutritionFilter::setMoreLess(CRecipeFilter::EMoreLessType moreLess) {
    if (m_more_less != moreLess) {
        m_more_less = moreLess;
    }
}

double CNutritionFilter::quantity() {
    return m_quantity;
}

void CNutritionFilter::setQuantity(double quantity) {
    if (m_quantity != quantity) {
        m_quantity = quantity;
    }
}


CNutritionFilter::ERecipeFilterType CNutritionFilter::recipeFilterType() {
    return s_recipe_filter_type;
}

void CNutritionFilter::setRecipeFilterType(ERecipeFilterType recipeFilterType) {
    if (s_recipe_filter_type != recipeFilterType) {
        s_recipe_filter_type = recipeFilterType;
    }
}
